package cn.uniquesoft.controller;

import cn.uniquesoft.controller.base.BaseController;
import cn.uniquesoft.manager.ClientManager;
import cn.uniquesoft.page.DataGrid;
import cn.uniquesoft.service.IVisitProblemService;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.UserVO;
import cn.uniquesoft.vo.VisitProblemVO;
import cn.uniquesoft.vo.base.BaseVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/visitproblem")
public class VisitProblemController extends BaseController<VisitProblemVO> {
    private IVisitProblemService visitProblemService;
    public VisitProblemController(IVisitProblemService visitProblemService){
        super();
        this.baseService=visitProblemService;
        this.visitProblemService=visitProblemService;
        this.indexName="visitproblem/index";
    }

    @RequestMapping(value = "/pageQuery")
    @ResponseBody
    public DataGrid pageQuery(VisitProblemVO item) {
        DataGrid dg = new DataGrid();
        UserVO userVO = null;
        try {
            userVO = ClientManager.getInstance().getClient().getUser();
        } catch (Exception e) {

        }
        if (userVO != null) {
            if (userVO.getIvisible() == 1) {

            } else if (userVO.getIvisible() == 2) {
                item.setIbank(userVO.getIorgname());
            } else {
                item.setIcreator(userVO.getIid());
            }
        }
        return this.baseService.pageQuery(item);
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public AnswerVO save(VisitProblemVO item) {
        AnswerVO answerVO = new AnswerVO();
        UserVO userVO = null;
        try {
            userVO = ClientManager.getInstance().getClient().getUser();
        } catch (Exception e) {

        }
        item.setIbank(userVO.getIorgname());
        item.setCbank(userVO.getCorgname());
        try {

            this.baseService.save(item);
            int itemId = ((BaseVO) item).getIid();
            if (itemId > 0) {
                answerVO.setSuccess(true);
                answerVO.setMsg("新增成功!");
            } else {
                answerVO.setSuccess(false);
                answerVO.setMsg("新增失败!");
            }
            answerVO.setData(itemId);
        } catch (Exception e) {
            answerVO.setSuccess(false);
            answerVO.setMsg("新增失败," + e.getMessage());
        }

        return answerVO;
    }
}
